<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
  <title>图书管理系统</title>
  <style>
    .avatar{
      width: 50px;
      height: 30px;
    }
    body {
      font-family: Arial, sans-serif;
      margin: 20px;
    }
    .container {
      width: 80%;
      margin: 0 auto;
    }
    h1 {
      text-align: center;
      color: #333;
    }
    .action-buttons {
      margin: 20px 0;
      text-align: right;
    }
    .book-table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
    }
    .book-table th, .book-table td {
      border: 1px solid #ddd;
      padding: 12px;
      text-align: left;
    }
    .book-table th {
      background-color: #f2f2f2;
      font-weight: bold;
    }
    .book-table tr:nth-child(even) {
      background-color: #f9f9f9;
    }
    .book-table tr:hover {
      background-color: #f1f1f1;
    }
    .btn {
      padding: 6px 12px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      text-decoration: none;
      font-size: 14px;
    }
    .btn-edit {
      background-color: #2196F3;
    }
    .btn-delete {
      background-color: #f44336;
    }
    .btn-add {
      background-color: #4CAF50;
    }

    /* 新增的分页样式 */
    .pagination-container {
      margin-top: 20px;
      text-align: center;
    }
    .pagination-info {
      margin-bottom: 10px;
      font-size: 14px;
      color: #555;
    }
    .pagination {
      display: inline-block;
      padding-left: 0;
      margin: 0;
      border-radius: 4px;
    }
    .pagination a {
      color: #333;
      padding: 6px 12px;
      text-decoration: none;
      border: 1px solid #ddd;
      margin: 0 3px;
      border-radius: 3px;
      transition: all 0.3s;
    }
    .pagination a:hover {
      background-color: #f5f5f5;
      color: #23527c;
    }
    .pagination a.active {
      background-color: #337ab7;
      color: white;
      border-color: #337ab7;
    }
    .pagination a.disabled {
      color: #777;
      cursor: not-allowed;
      background-color: #fff;
      border-color: #ddd;
    }
    /* 添加图书表单样式 */
    .addDivNone {
      display: none; /* 默认隐藏 */
      margin: 20px auto;
      width: 50%;
      padding: 20px;
      background: #f9f9f9;
      border-radius: 5px;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }

    .addForm {
      width: 100%;
      border-collapse: collapse;
    }

    .addForm td {
      padding: 10px;
      border-bottom: 1px solid #eee;
    }

    .addForm tr:last-child td {
      border-bottom: none;
    }

    .addForm tr:first-child td {
      font-weight: bold;
      font-size: 18px;
      text-align: center;
      border-bottom: 2px solid #4CAF50;
    }

    .right {
      text-align: right;
      width: 30%;
    }

    .left {
      text-align: left;
    }

    /* 添加按钮样式 */
    .btn-add {
      padding: 8px 16px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      text-decoration: none;
      font-size: 14px;
      transition: all 0.3s;
    }

    .btn-add:hover {
      background-color: #45a049;
    }

    /* 表单输入框样式 */
    .addForm input[type="text"],
    .addForm input[type="password"] {
      width: 100%;
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
    }

    .addForm input[type="submit"],
    .addForm input[type="reset"] {
      padding: 8px 16px;
      margin: 0 10px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    .addForm input[type="submit"] {
      background-color: #4CAF50;
      color: white;
    }

    .addForm input[type="reset"] {
      background-color: #f44336;
      color: white;
    }
    /* 头部导航栏样式 */
    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 20px;
      background-color: #f8f9fa;
      border-bottom: 1px solid #e1e1e1;
      margin-bottom: 20px;
    }

    .header a {
      color: #007bff;
      text-decoration: none;
      margin: 0 5px;
      font-size: 14px;
      color: black;
    }

    .header a:hover {
      color: red;
      text-decoration: underline;
    }

    /* 登录信息样式 */
    .header-right {
      color: #6c757d;
      font-size: 14px;
    }

    .header header-right a {
      color: #007bff;
      text-decoration: none;
      margin: 0 5px;
      font-size: 14px;
      color: black;
    }
    /* 搜索容器样式 */
    .search-container {
      margin: 20px 0;
      padding: 20px;
      background-color: #f8f9fa;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    /* 搜索框整体样式 */
    .search-box {
      max-width: 1000px;
      margin: 0 auto;
    }

    /* 搜索字段容器 */
    .search-fields {
      display: flex;
      flex-wrap: wrap;
      gap: 15px;
      align-items: flex-end; /* 使元素底部对齐 */
    }

    /* 单个搜索字段样式 */
    .search-field {
      flex: 1;
      min-width: 200px;
    }

    /* 带按钮的搜索字段容器 */
    .search-field-with-buttons {
      display: flex;
      gap: 10px;
      align-items: flex-end;
    }

    .search-field label {
      display: block;
      margin-bottom: 6px;
      font-weight: 500;
      color: #495057;
      font-size: 14px;
    }

    .search-field input {
      width: 100%;
      padding: 10px 12px;
      border: 1px solid #ced4da;
      border-radius: 4px;
      font-size: 14px;
      transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    }

    .search-field input:focus {
      border-color: #80bdff;
      outline: 0;
      box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
    }

    /* 搜索按钮区域 */
    .search-actions {
      display: flex;
      gap: 10px;
      /*margin-bottom: 6px; !* 与输入框对齐 *!*/
      /*border: 1px solid #ced4da;*/
    }

    /* 按钮样式 */
    .btn-search, .btn-reset {
      margin-top: 15px;
      padding: 10px 20px;
      /*height: 40px; !* 与输入框高度一致 *!*/
      border: none;
      border-radius: 4px;
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
    }

    .btn-search {
      background-color: #007bff;
      color: white;
    }

    .btn-search:hover {
      background-color: #0069d9;
    }

    .btn-reset {
      background-color: #6c757d;
      color: white;
    }

    .btn-reset:hover {
      background-color: #5a6268;
    }

    /* 响应式调整 */
    @media (max-width: 768px) {
      .search-fields {
        flex-direction: column;
        gap: 10px;
      }

      .search-field, .search-field-with-buttons {
        min-width: 100%;
      }

      .search-field-with-buttons {
        flex-direction: column;
      }

      .search-actions {
        justify-content: flex-end;
        margin-top: 10px;
      }
    }
  </style>
</head>
<body>
<!-- 新增的头部导航栏 -->
<div class="header">
  <div class="header-right">
    <c:if test="${not empty sessionScope.username}">
      欢迎您：${username} &nbsp;&nbsp; 【<a href="reader?action=logout">注销</a>】
    </c:if>
    <c:if test="${empty sessionScope.username}">
      【<a href="login.jsp" style="color: black">请登录</a>】【<a href="register.jsp" style="color: black"  >请注册</a>】
    </c:if>
  </div>
  <div class="action-buttons">
    <a href="javascript:void(0)" class="btn-add" onclick="toggleAddForm()">添加图书</a>
  </div>
</div>

<div id="addDiv" class="addDivNone">
  <form action="book.do?action=addBook" method="post" onsubmit="return doAddBook();">
    <table class="addForm">
      <tr>
        <td colspan="2">添加图书hhhh</td>
      </tr>
      <tr>
        <td class="right">书名：</td>
        <td class="left"><input type="text" name="title" required></td>
      </tr>
      <tr>
        <td class="right">作者：</td>
        <td class="left"><input type="text" name="author" required></td>
      </tr>
      <tr>
        <td class="right">是否可借：</td>
        <td class="left">
          <select name="available">
            <option value="true">可借</option>
            <option value="false">不可借</option>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
          <input type="reset" value="重置">
          <input type="submit" value="提交">
        </td>
      </tr>
    </table>
  </form>
</div>
<div class="search-container">
  <div class="search-box">
    <form id="pageForm" action="book?action=findAllBooks" onsubmit="return Quary()" method="post">
      <div class="search-fields">
        <div class="search-field">
          <label for="search-id">编号</label>
          <input type="text" id="search-id" name="id" placeholder="请输入编号" value="${id}">
        </div>
        <div class="search-field">
          <label for="search-title">书名</label>
          <input type="text" id="search-title" name="title" placeholder="请输入书名" value="${title}">
        </div>
        <div class="search-field-with-buttons">
          <div class="search-field">
            <label for="search-author">作者</label>
            <input type="text" id="search-author" name="author" placeholder="请输入作者" value="${author}">
          </div>
          <div class="search-actions">
            <button type="submit" class="btn-search">查询</button>
            <button type="button" class="btn-reset" onclick="location.href='book?action=findAllBooks'">重置</button>
          </div>
        </div>
      </div>
      <input type="hidden" id="pageNo" name="pageNo" value="${pageBean.pageNo}">
      <input type="hidden" id="pageSize" name="pageSize" value="${pageBean.pageSize}">
      <input type="hidden" id="totalPage" name="totalPage" value="${pageBean.totalPage}">
    </form>
  </div>
</div>
<div class="container">
  <h1>图书列表</h1>
  <table class="book-table">
    <thead>
    <tr>
      <th>Cover</th>
      <th>ID</th>
      <th>书名</th>
      <th>作者</th>
      <th>是否可借</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${pageBean.list}" var="book">
      <tr>
        <td><img class="avatar" src="http://192.168.138.3:9000/avatars/${book.cover}" alt=""></td>
        <td>${book.id}</td>
        <td>${book.title}</td>
        <td>${book.author}</td>
        <td>${book.available}</td>
        <td>
          <a href="/book.do?action=findBookById&id=${book.id}" class="btn btn-edit">编辑</a>
          <a href="/book.do?action=deleteBook&id=${book.id}"
             onclick="return confirm('确定要删除《${book.title}》吗？')"
             class="btn btn-delete">删除</a>
        </td>
      </tr>
    </c:forEach>
    </tbody>
  </table>

  <!-- 美化后的分页部分 -->
  <div class="pagination-container">
    <div class="pagination-info">
      总记录数：${pageBean.totalCount} / 总页数：${pageBean.totalPage} / 当前页：${pageBean.pageNo}
    </div>
    <div class="pagination">
      <a href="javascript:jumpPage('1')" class="${pageBean.pageNo == 1 ? 'disabled' : ''}">首页</a>
      <a href="javascript:jumpPage('${pageBean.pageNo-1}')" class="${pageBean.pageNo == 1 ? 'disabled' : ''}">上一页</a>

      <c:forEach begin="1" end="${pageBean.totalPage}" var="i">
        <a href="javascript:jumpPage('${i}')" class="${pageBean.pageNo == i ? 'active' : ''}">${i}</a>
      </c:forEach>

      <a href="javascript:jumpPage('${pageBean.pageNo+1}')" class="${pageBean.pageNo == pageBean.totalPage ? 'disabled' : ''}">下一页</a>
      <a href="javascript:jumpPage('${pageBean.totalPage}')" class="${pageBean.pageNo == pageBean.totalPage ? 'disabled' : ''}">末页</a>
    </div>
  </div>

  <c:if test="${not empty msg}">
    <div style="text-align: center; margin-top: 15px;">
      <span style="color: red">${msg}</span>
    </div>
  </c:if>
</div>
</body>
<script>

  function Quary(){
    //提交表单前，将当前页设置为1
    document.getElementById('pageNo').value=1;
    return true;
  }
  //分页函数
  function jumpPage(pageNo){
    //pageNo是文本字符串，需要转为int
    let no = parseInt(pageNo);
    //左边界处理
    if(no <= 1){
      no = 1;
    }
    //有边界处理
    //取得最大页数
    let totalPage = parseInt(document.getElementById('totalPage').value);
    if(no >= totalPage){
      no = totalPage;
    }
    //将当前页设置到表单元素中
    document.getElementById('pageNo').value=no;
    //让pageForm提交
    document.getElementById('pageForm').submit();
  }
  function toggleAddForm() {
    const addDiv = document.getElementById('addDiv');
    if (addDiv.style.display === 'block') {
      addDiv.style.display = 'none';
    } else {
      addDiv.style.display = 'block';
    }
  }
</script>
</html>